package SqList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


public class TestSqList {
    public static void main(String[] args) {
        //题：用集合实现字符串去除子串
        ArrayList<Character> stringArrayList = new ArrayList<>();
        String s1 = "welcome to China";
        String s2 = "Come";

        int len1 = s1.length();
        for (int i = 0; i < len1; i++) {
            char ch = s1.charAt(i);
            // 重点注意：使用字符串中是否有该字符，用字符串.Contains(字符+"")，需要传进去一个字符串
            if(!s2.contains(ch + "")) {
                stringArrayList.add(ch);
            }
        }
        for (Character character : stringArrayList) {
            System.out.print(character);
        }
    }


    public static void main1(String[] args) {
        ArrayList<Person> personArrayList = new ArrayList<>();
        Person person = new Person("张三","男",23);
        personArrayList.add(person);
        personArrayList.add(new Person("李四","男",30));
        personArrayList.add(new Person("玛丽","女",27));

        //给对象排序：
        // 1、Person实现Comparable接口
        // 2、Person重写CompareTo方法，确定以什么比较
        // 3、先import引入java.util包中的，Collections类
        // 4、调用Collections的静态方法sort，会调用重写的CompareTo方法，实现比较，从而实现排序
        Collections.sort(personArrayList);

        //遍历：
        //1、重写类中toString
        //2、用Fori遍历
        //3、用ForEach遍历
        //4、用迭代器
        //迭代器实现遍历
        // 1、import引进java.util包下的Iterator类
        // 2、定义一个Iterator<Person>的引用，将personArrayList的迭代器给这个引用
        Iterator<Person> iterator1 = personArrayList.iterator();
        // 3、下一个有数据，就进行这一步，否则退出
        while(iterator1.hasNext()){
            System.out.println(iterator1.next());
        }

        //迭代器实现ArrayList的遍历
        Iterator<Person> iterable = personArrayList.iterator();
        while(iterable.hasNext()) {
            System.out.println(iterable.next()+" ");
        }
        System.out.println();


        for (Person person1 : personArrayList) {
            System.out.println(person1);
        }
        System.out.println("========================");

        //List是ArrayList的父类
        List<Person> people = personArrayList.subList(0, 2);
        for (Person person1 : people) {
            System.out.println(person1);
        }
        people.set(0,new Person("zhangsan","nan",18));
        System.out.println("=========================");

        for (Person person1 : people) {
            System.out.println(person1);
        }

    }
}

